import { HttpServiceResponse, Model, PageInfo } from '../services/core';

export const HttpUtil = {
	getCode: <T>(response: HttpServiceResponse<T>): number => response.data.code,

	getMessage: <T>(response: HttpServiceResponse<T>): string =>
		response.data.msg,

	getData: <T>(response: HttpServiceResponse<T>): T => response.data.data,

	setData: <T>(response: HttpServiceResponse<T>, data: T): void => {
		response.data.data = data;
	},

	getList: <T extends Model>(response: HttpServiceResponse<PageInfo<T>>): T[] =>
		response.data.data.records,

	setList: <T extends Model>(
		response: HttpServiceResponse<PageInfo<T>>,
		records: T[]
	): void => {
		response.data.data.records = records;
	},

	getPageInfo: <T extends Model>(
		response: HttpServiceResponse<PageInfo<T>>
	): PageInfo<T> => response.data.data,

	getHeaders: <T = any>(response: HttpServiceResponse<T>): string | undefined =>
		response.headers,

	getHeader: <T = any>(
		response: HttpServiceResponse<T>,
		headerName: string
	): string | undefined => response.headers[headerName.toLowerCase()],

	isOk: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status === 200,

	isCreated: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status === 201,

	isNoContent: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status === 204,

	is2xx: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status >= 200 && response.status < 300,

	isBadRequest: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status === 400,

	isUnauthorized: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status === 401,

	isForbidden: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status === 403,

	isNotFound: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status === 404,

	is4xx: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status >= 400 && response.status < 500,

	is5xx: <T = any>(response: HttpServiceResponse<T>): boolean =>
		response.status >= 500 && response.status < 600,

	post: (url: string, params?: { [key: string]: any }) => {
		const formElement = document.createElement('form');
		formElement.action = url;
		formElement.target = '_self';
		formElement.method = 'post';
		formElement.style.display = 'none';
		if (params) {
			Object.keys(params).forEach(value => {
				const opt = document.createElement('textarea');
				opt.name = value;
				opt.value = params[value];
				formElement.appendChild(opt);
			});
		}
		document.body.appendChild(formElement);
		formElement.submit();
		document.body.removeChild(formElement);
	},

	getParams: (url: string): { [key: string]: unknown } => {
		const param = {};
		url.replace(/([^?&]+)=([^?&]+)/g, function (s, v, k) {
			const val = decodeURIComponent(k);
			const num = Number(val);
			if (isNaN(num)) {
				if (val === 'true') {
					param[v] = true;
				} else if (val === 'false') {
					param[v] = false;
				} else {
					param[v] = val;
				}
			} else {
				param[v] = num;
			}
			return k + '=' + v;
		});
		return param;
	},

	redirect: (redirect: string): void => {
		window.location.href = redirect;
	},

	reload: (): void => {
		window.location.reload();
	},

	isExternal: (path: string): boolean => {
		return /^(http:|https:|mailto:|tel:)/.test(path);
	},
};
